package com.unience.ec2.eclipse.dialog;

import java.util.HashMap;
import java.util.List;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;

import com.unience.ec2.eclipse.config.UtilSWT;
import com.unience.ec2.eclipse.config.account.Account;
import com.unience.ec2.eclipse.config.account.AccountManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GetAccountDialog extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Button buttonOK;
	private Button buttonNew;
	private Button buttonCancel;
	private Combo comboAccounts;
	
	private boolean accept = true;
	
	private Account account;
	
	private HashMap<String,Account> accountMap;

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Dialog inside a new Shell.
	*/
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			GetAccountDialog inst = new GetAccountDialog(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public GetAccountDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			AccountManager accountManager = new AccountManager();
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

			dialogShell.setLayout(new FormLayout());
			dialogShell.layout();
			dialogShell.pack();			
			dialogShell.setSize(394, 160);
			dialogShell.setText("Select Ec2 Account");
			{
				buttonNew = new Button(dialogShell, SWT.TOGGLE | SWT.CENTER);
				buttonNew.setText("New");
				FormData buttonNewLData = new FormData();
				buttonNewLData.width = 47;
				buttonNewLData.height = 29;
				buttonNewLData.left =  new FormAttachment(0, 1000, 279);
				buttonNewLData.top =  new FormAttachment(0, 1000, 46);
				buttonNew.setLayoutData(buttonNewLData);
				buttonNew.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {			
						
						NewAccountDialog inst = new NewAccountDialog(UtilSWT.getShell(),
								SWT.NULL);

						inst.open();

						if (inst.isAccepted()) {
							AccountManager accountManager = new AccountManager();
							Account account = new Account();

							account.setName(inst.getName());
							account.setKey(inst.getKey());
							account.setSecret(inst.getSecret());
							account.setOwnerId(inst.getOwnerID());
							accountManager.addAccount(account);
							
							List<Account> accountList =  accountManager.getAccountList();
							comboAccounts.removeAll();
							accountMap = new HashMap<String, Account>();
							
							for (Account acc : accountList) {
								comboAccounts.add(acc.getName());
								accountMap.put(acc.getName(), acc);
							}							

						}
					}
				});
			}
			{
				buttonCancel = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				buttonCancel.setText("Cancel");
				FormData buttonCancelLData = new FormData();
				buttonCancelLData.width = 55;
				buttonCancelLData.height = 29;
				buttonCancelLData.left =  new FormAttachment(0, 1000, 273);
				buttonCancelLData.top =  new FormAttachment(0, 1000, 87);
				buttonCancel.setLayoutData(buttonCancelLData);
				buttonCancel.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {			
						accept = false;
						dialogShell.close();
						
						
					}
				});
				
			}
			{
				buttonOK = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				buttonOK.setText("Ok");
				FormData buttonOKLData = new FormData();
				buttonOKLData.width = 65;
				buttonOKLData.height = 29;
				buttonOKLData.left =  new FormAttachment(0, 1000, 202);
				buttonOKLData.top =  new FormAttachment(0, 1000, 87);
				buttonOK.setLayoutData(buttonOKLData);
				
				
				buttonOK.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {			
						
						String text = comboAccounts.getText();
						account = accountMap.get(text);
						dialogShell.close();
					}
				});
				
			}
			
		
			{
				comboAccounts = new Combo(dialogShell, SWT.NONE);
				
				FormData comboAccountsLData = new FormData();
				comboAccountsLData.width = 225;
				comboAccountsLData.height = 29;
				comboAccountsLData.left =  new FormAttachment(0, 1000, 48);
				comboAccountsLData.top =  new FormAttachment(0, 1000, 42);
				comboAccounts.setLayoutData(comboAccountsLData);
			}
			
			
			
			List<Account> accountList =  accountManager.getAccountList();
			
			accountMap = new HashMap<String, Account>();
			
			for (Account account : accountList) {
				comboAccounts.add(account.getName());
				accountMap.put(account.getName(), account);
			}
			
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isAccept() {
		return accept;
	}

	public void setAccept(boolean accept) {
		this.accept = accept;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}
	
}
